package com.cenwangkeji.resident.ui.activity;

import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;

import com.cenwangkeji.resident.R;
import com.cenwangkeji.resident.model.Constant;
import com.cenwangkeji.resident.model.OkHttpUtils;
import com.cenwangkeji.resident.model.bean.ResidentDetailBean;
import com.cenwangkeji.resident.model.bean.User;
import com.cenwangkeji.resident.ui.adapter.SpecialDiseaseDetailAdapter;
import com.google.gson.Gson;
import com.google.gson.GsonBuilder;
import com.lws.basic.base.BaseActivity;
import com.lws.basic.persenter.ObserverOnNext;
import com.lws.basic.view.adapter.AdapterIntegerDefault0;
import com.lws.basic.view.adapter.DoubleDefault0Adapter;
import com.lws.basic.utils.common.MyLogger;
import com.lws.basic.utils.common.MyToast;
import com.lws.basic.utils.common.RxUtils;
import com.tencent.bugly.crashreport.CrashReport;

import org.json.JSONObject;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import butterknife.BindView;
import butterknife.ButterKnife;
import io.reactivex.Observable;
import io.reactivex.ObservableEmitter;
import io.reactivex.ObservableOnSubscribe;

public class InfoHealthDetailActivity extends BaseActivity {


    @Override
    public int setLayoutId() {
        return R.layout.activity_info_health_detail;
    }
    @BindView(R.id.rv)
    RecyclerView mRv;
    private SpecialDiseaseDetailAdapter mAdapter;

    @Override
    public void initData() {
        super.initData();
        ButterKnife.bind(this);
        initRecycelrView();
        getData();
    }

    private void getData(){
        showLoading();
        Observable.create(new ObservableOnSubscribe<ResidentDetailBean>() {
            @Override
            public void subscribe(ObservableEmitter<ResidentDetailBean> emitter) throws Exception {

                Map<Object, Object> map = new HashMap<>();
                map.put("token", User.getToken());
                map.put("mark", 3);
                map.put("jmId", User.getUserId());
                JSONObject jsonObject = new JSONObject(map);
                MyLogger.i(jsonObject.toString());
                Gson gson = new GsonBuilder()
                        .registerTypeAdapter(Integer.class, new AdapterIntegerDefault0())
                        .registerTypeAdapter(int.class, new AdapterIntegerDefault0())
                        .registerTypeAdapter(Double.class, new DoubleDefault0Adapter())
                        .registerTypeAdapter(double.class, new DoubleDefault0Adapter())
                        .create();
                String json = OkHttpUtils.post( jsonObject.toString(),Constant.resident_detail);
                MyLogger.i(json);
                emitter.onNext(gson.fromJson(json,ResidentDetailBean.class));
            }
        }).compose(RxUtils.<ResidentDetailBean>io_main()).subscribe(new ObserverOnNext<ResidentDetailBean>() {
            @Override
            public void onNext(ResidentDetailBean residentDetailBean) {
                try {
                    dismissLoading();
                    if (residentDetailBean.getCode() == 0 && residentDetailBean.getData() != null) {
                        ResidentDetailBean.DataBean dataBean = residentDetailBean.getData();
                        List<String> list = new ArrayList<>();
                        list.add("药物过敏史:\n          " + ywgms(User.numList(dataBean.getYwgms()), dataBean.getYwgmsQt()));
                        list.add("暴露史:\n          " + bls(User.numList(dataBean.getBls())));
                        if (dataBean.getJb().equals("2")) {
                            String jb = "";
                            if (dataBean.getJbGxy() == 1) {
                                jb += "     高血压 确诊时间" + dataBean.getJbGxyQzsj() + "\n";
                            }
                            if (dataBean.getJbTnb() == 1) {
                                jb += "     糖尿病 确诊时间" + dataBean.getJbTnbQzsj() + "\n";
                            }
                            if (dataBean.getJbGxb() == 1) {
                                jb += "     冠心病 确诊时间" + dataBean.getJbGxbQzsj() + "\n";
                            }
                            if (dataBean.getJbMxzsxfjb() == 1) {
                                jb += "     慢性阻塞性肺疾病 确诊时间" + dataBean.getJbMxzsxfjbQzsj() + "\n";
                            }
                            if (dataBean.getJbExzl() == 1) {
                                jb += "     恶性肿瘤 确诊时间" + dataBean.getJbExzlQzsj() + "位置" + dataBean.getJbExzlWz() + "\n";
                            }
                            if (dataBean.getJbNcz() == 1) {
                                jb += "     脑卒中 确诊时间" + dataBean.getJbNczQzsj() + "\n";
                            }
                            if (dataBean.getJbYzjsza() == 1) {
                                jb += "     严重精神障碍 确诊时间" + dataBean.getJbYzjszaQzsj() + "\n";
                            }
                            if (dataBean.getJbJhb() == 1) {
                                jb += "     结核病 确诊时间" + dataBean.getJbJhbQzsj() + "\n";
                            }
                            if (dataBean.getJbGy() == 1) {
                                jb += "     肝炎 确诊时间" + dataBean.getJbGyQzsj() + "\n";
                            }
                            if (dataBean.getJbQtfdcrb() == 1) {
                                jb += "     其他法定传染病 确诊时间" + dataBean.getJbQtfdcrbQzsj() + "\n";
                            }
                            if (dataBean.getJbZyb() == 1) {
                                jb += "     职业病 确诊时间" + dataBean.getJbZybQzsj() + "位置：" + dataBean.getJbZybWz() + "\n";
                            }
                            if (dataBean.getJbQt() == 1) {
                                jb += "     其他确诊时间" + dataBean.getJbQtQzsj() + "\n";
                            }
                            list.add("疾病:\n     " + jb);

                        }
                        if (dataBean.getSs() == 2) {

                            list.add("手术名称1:  " + dataBean.getSsMc1());
                            list.add("手术时间1:  " + dataBean.getSsSj1());
                            list.add("手术名称2:  " + dataBean.getSsMc2());
                            list.add("手术时间2:  " + dataBean.getSsSj2());
                        }
                        if (dataBean.getWs() == 2) {
                            list.add("外伤名称1:  " + dataBean.getWsMc1());
                            list.add("外伤时间1:  " + dataBean.getWsSj1());
                            list.add("外伤名称2:  " + dataBean.getWsMc2());
                            list.add("外伤时间2:  " + dataBean.getWsSj2());
                        }
                        if (dataBean.getSx() == 2) {
                            list.add("输血原因1:  " + dataBean.getSxYy1());
                            list.add("输血时间1:  " + dataBean.getSxSj1());
                            list.add("输血原因2:  " + dataBean.getSxYy2());
                            list.add("输血时间2:  " + dataBean.getSxYy2());
                        }
                        list.add("家族病史父亲:\n          " + jzbs(User.numList(dataBean.getJzsFq()), dataBean.getJzsFqQt()));
                        list.add("家族病史母亲:\n          " + jzbs(User.numList(dataBean.getJzsMq()), dataBean.getJzsMqQt()));
                        list.add("家族病史兄弟姐妹:\n          " + jzbs(User.numList(dataBean.getJzsXdjm()), dataBean.getJzsXdjmQt()));
                        list.add("家族病史子女:\n          " + jzbs(User.numList(dataBean.getJzsZn()), dataBean.getJzsZnQt()));
                        list.add("遗传病史:  " + ycbs(dataBean.getYcbs(), dataBean.getYcbsJbmc()));
                        list.add("残疾情况:  " + cjqk(User.numList(dataBean.getCjqk()), dataBean.getCjqkQtcj()));
                        list.add("厨房排风设施:  " + cf(dataBean.getCfpfss()));
                        list.add("燃料类型:  " + rl(dataBean.getRllx()));
                        list.add("饮水:  " + ys(dataBean.getRs()));
                        list.add("厕所:  " + cs(dataBean.getCs()));
                        list.add("禽畜栏:  " + qcl(dataBean.getQcl()));
                        mAdapter.setNewData(list);

                    } else {
                        MyToast.show(residentDetailBean.getMsg());
                    }
                }catch (Exception e){
                    MyToast.show(e.getMessage());
                    CrashReport.postCatchedException(e);
                }
            }
        });
    }

    //qcl
    private String qcl(int index) {
        String s = "";
        switch (index) {
            case 1:
                s = "无";
                break;
            case 2:
                s = "单设";
                break;
            case 3:
                s = "室内";
                break;
            case 4:
                s = "室外";
                break;
            case 5:
                s = "简易棚厕";
                break;

        } return s;
    }
    //厕所
    private String cs(int index) {
        String s = "";
        switch (index) {
            case 1:
                s = "卫生厕所";
                break;
            case 2:
                s = "一格或二格粪池式";
                break;
            case 3:
                s = "马桶";
                break;
            case 4:
                s = "露天粪坑";
                break;
            case 5:
                s = "简易棚厕";
                break;



        } return s;
    }
    //饮水
    private String ys(int index) {
        String s = "";
        switch (index) {
            case 1:
                s = "自来水";
                break;
            case 2:
                s = "经净化过滤的水";
                break;
            case 3:
                s = "井水";
                break;
            case 4:
                s = "河湖水";
                break;
            case 5:
                s = "塘水";
                break;
            case 6:
                s = "其他";
                break;


        } return s;
    }
    //燃料
    private String rl(int index) {
        String s = "";
        switch (index) {
            case 1:
                s = "液化气";
                break;
            case 2:
                s = "煤";
                break;
            case 3:
                s = "天然气";
                break;
            case 4:
                s = "沼气";
                break;
            case 5:
                s = "柴火";
                break;
            case 6:
                s = "其他";
                break;


        } return s;
    }
    //厨房
    private String cf(int index) {
        String s = "";
        switch (index) {
            case 1:
                s = "无";
                break;
            case 2:
                s = "油烟机";
                break;
            case 3:
                s = "换气扇";
                break;
            case 4:
                s = "烟囱";
                break;


        } return s;
    }

    //残疾情况
    private  String cjqk(List<String> list, String qt){
        String s ="";
        for (int i=0; i< list.size(); i++) {
            switch (list.get(i)) {
                case "1":
                    s += "无残疾  ";
                    break;
                case "2":
                    s += "视力残疾  ";
                    break;
                case "3":
                    s += "听力残疾  ";
                    break;
                case "4":
                    s += "言语残疾  ";
                    break;
                case "5":
                    s += "肢体残疾  ";
                    break;
                case "6":
                    s += "智力残疾  ";
                    break;
                case "7":
                    s += "精神残疾  ";
                    break;
                case "8":
                    s += qt;
                    break;

            }
        }
        return s;

    }


    //遗传病史
    private String ycbs(int index,String mc){
        String s = "";
        switch (index){
            case 1:
                s ="无" ;
                break;
            case 2:
                s = mc;
                break;
        }
        return s;

    }
    //家族病史
    private  String jzbs(List<String> list,String qt){
        String s ="";
        for (int i=0; i< list.size(); i++) {
            switch (list.get(i)) {
                case "1":
                    s += "无  ";
                    break;
                case "2":
                    s += "高血压  ";
                    break;
                case "3":
                    s += "糖尿病  ";
                    break;
                case "4":
                    s += "冠心病  ";
                    break;
                case "5":
                    s += "慢性阻塞性肺疾病  ";
                    break;
                case "6":
                    s += "恶性肿瘤  ";
                    break;
                case "7":
                    s += "脑卒中  ";
                    break;
                case "8":
                    s += "严重精神障碍 ";
                    break;
                case "9":
                    s += "结核病  ";
                    break;
                case "10":
                    s += "肝炎  ";
                    break;
                case "11":
                    s += "先天畸形  ";
                    break;
                case "12":
                    s += qt;
                    break;

            }
        }
        return s;

    }


    //疾病
    private  String jb(List<String> list,String sj2,String sj3,String sj4, String sj5,
                       String sj6, String sj7, String sj8,  String sj9, String
                               sj10, String sj11, String sj12, String sj13,String zlwz,String zybwz,  String qt){
        String s ="";
        for (int i=0; i< list.size(); i++) {
            switch (list.get(i)) {
                case "1":
                    s += "无  ";
                    break;
                case "2":
                    s += "高血压  ";
                    break;
                case "3":
                    s += "糖尿病  ";
                    break;
                case "4":
                    s += "冠心病  ";
                    break;
                case "5":
                    s += "慢性阻塞性肺疾病  ";
                    break;
                case "6":
                    s += "恶性肿瘤  "+zlwz;
                    break;
                case "7":
                    s += "脑卒中  ";
                    break;
                case "8":
                    s += "严重精神病障碍  ";
                    break;
                case "9":
                    s += "结核病  ";
                    break;
                case "10":
                    s += "肝炎  ";
                    break;
                case "11":
                    s += "其他法定传染病  ";
                    break;
                case "12":
                    s += "职业病 "+zybwz;
                    break;
                case "13":
                    s += qt;

            }
        }
        return s;

    }


    //药物过敏史
    private  String ywgms(List<String> list, String qt){
        String s ="";
        for (int i=0; i< list.size(); i++) {
            switch (list.get(i)) {
                case "0":
                    s += "无  ";
                    break;
                case "1":
                    s += "青霉素  ";
                    break;
                case "2":
                    s += "磺胺  ";
                    break;
                case "3":
                    s += "链霉素  ";
                    break;
                case "4":
                    s += qt;

            }
        }
        return s;

    }
    //暴露史
    private  String bls(List<String> list){
        String s ="";
        for (int i=0; i< list.size(); i++) {
            switch (list.get(i)) {
                case "0":
                    s += "无  ";
                    break;
                case "1":
                    s += "化学品  ";
                    break;
                case "2":
                    s += "磺胺  ";
                    break;
                case "3":
                    s += "毒物  ";
                    break;
                case "4":
                    s += "射线  ";

            }
        }
        return s;

    }

    private void initRecycelrView() {
        mRv.setLayoutManager(new LinearLayoutManager(this));
        mAdapter = new SpecialDiseaseDetailAdapter(null);
        mRv.setAdapter(mAdapter);
    }
}
